import http from "@/api/axios";
import { useBasicDataStore } from "@/stores/basicDataStore";
import { useCmpDataStore } from "@/stores/cmpDataStore";
import { useLoadStore } from "@/stores/loadState";
import { useEffect } from "react";

export const useInitData = () => {
  const setBasicData = useBasicDataStore((state) => state.setBasicData);
  const setCmpData = useCmpDataStore((state) => state.setCmpData);
  const basicData = useBasicDataStore((state) => state.basicData);
  const cmpData = useCmpDataStore((state) => state.cmpData);
  const setLoaded = useLoadStore((state) => state.setLoaded);

  useEffect(() => {
    const loadData = async () => {
      if (basicData === null || cmpData === null) {
        try {
          const res = await http.get('/api/storesData');
          const data = res.data.data[0];
          setBasicData(data);
          setCmpData(data);
        } catch (error) {
          console.error('初始化数据失败:', error);
        } finally {
          setLoaded(false); // 无论成功与否都更新状态
        }
      }
      else {
        setLoaded(false);
      }
    };
    loadData();

  }, []);

};